/**
 * @see [微信官方文档](https://mp.weixin.qq.com/debug/wxadoc/dev/api/systeminfo.html#wxgetsysteminfoobject)
 */
import get from '../cache/get';
import set from '../cache/set';

const WX_SYSTEM_INFO = 'feidao-wx-system-info';

export default async function get_system_info() {
	const system_info = await get(WX_SYSTEM_INFO) as wx.SystemInfo;
	if (!system_info) {
		console.log('could get systeminfo from storage, call wx.getSystemInfo');
		return await new Promise<wx.SystemInfo>((resolve, reject) => {
			wx.getSystemInfo({
				async success(res) {
					const s_i = res;
					try {
						await set(WX_SYSTEM_INFO, s_i);
					} finally {
						resolve(s_i);
					}
				},
				fail() {
					reject('get system info failed.');
				}
			});
		});
	} else {
		return system_info;
	}
}
